package com.blate.dogxiao.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.blate.dogxiao.R;
import com.blate.dogxiao.Repo;
import com.blate.dogxiao.activity.AboutUsActivity;
import com.blate.dogxiao.activity.FeedbackActivity;
import com.blate.dogxiao.activity.LoginActivity;
import com.blate.dogxiao.bean.UserBean;
import com.blate.dogxiao.databinding.FragmentMineBinding;
import com.blate.dogxiao.function.AutoIoScheduler;
import com.blate.dogxiao.tools.UserTools;
import com.bumptech.glide.Glide;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * @author Blate
 * Created in 2021/4/20
 */
public class MineFragment
        extends Fragment {

    private FragmentMineBinding mBinding;

    private UserBean mSelf;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentMineBinding.inflate(inflater, container, false);
        mBinding.setHost(this);
        initialize(inflater.getContext());
        return mBinding.getRoot();
    }

    private void initialize(Context context) {
        Observable.fromCallable(new Callable<UserBean>() {
            @Override
            public UserBean call() throws Exception {
                return Repo.getSelfBean(context);
            }
        })
                .compose(new AutoIoScheduler<>())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new Consumer<UserBean>() {
                    @Override
                    public void accept(UserBean userBean) throws Exception {
                        mSelf = userBean;
                        loadUserInfoToUi();
                    }
                });
    }

    private void loadUserInfoToUi() {
        boolean isLogin = UserTools.getLoginStatus(getContext());
        if (mSelf == null || !isLogin) {
            mBinding.civAvatarSmall.setImageResource(R.drawable.ic_avatar_default);
            mBinding.ivAvatarBig.setImageResource(R.drawable.ic_user_background);
            mBinding.tvNickname.setText("注册/登录");
            mBinding.tvPhoneNumber.setText("-");
        } else {
           /* Glide
                    .with(this)
                    .load(mSelf.avatar)
                    .into(mBinding.ivAvatarBig);*/
            Glide
                    .with(this)
                    .load(mSelf.avatar)
                    .into(mBinding.civAvatarSmall);
            mBinding.tvNickname.setText(mSelf.nickname);
            mBinding.tvPhoneNumber.setText(mSelf.phoneNumber);
        }
        mBinding.btLogout.setVisibility(isLogin ? View.VISIBLE : View.GONE);
    }

    public void onLoginClick() {
        Intent intent = new Intent(getContext(), LoginActivity.class);
        startActivityForResult(intent, 1001);
    }

    public void onFeedbackClick() {
        startActivity(new Intent(getContext(), FeedbackActivity.class));
    }

    public void onAboutUsClick() {
        startActivity(new Intent(getContext(), AboutUsActivity.class));
    }

    public void onLogoutClick() {
        mSelf = null;
        UserTools.setLoginStatus(getContext(), false);
        loadUserInfoToUi();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == AppCompatActivity.RESULT_OK) {
            if (requestCode == 1001) {
                if (data != null) {
                    UserTools.setLoginStatus(getContext(), data.getBooleanExtra("login", false));
                    loadUserInfoToUi();
                }
            }
        }
    }
}
